package com.hongxun.design.utils.soundUtil;

import android.media.MediaPlayer;
import android.net.Uri;

import com.hongxun.design.MyApp;
import com.hongxun.design.interfaces.IVoiceManager;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.utils.httputil.HttpStringUtil;


/**
 * Created by zerdoor_pc on 2016/4/21.
 */
public class UPlayer implements IVoiceManager {

    private final String TAG = UPlayer.class.getName();
    public String path;
    private static UPlayer intence;
    public MediaPlayer mPlayer;
    long duration=0;
    public UPlayer(String path) {
        //path= ClipPathUtil.getAPPPath() + MyApp.openIdent+ File.separator+"1473819970521.amr";
        this.path = path;
        MyLog.L(MString.Params + "pathnow:" + path);
        //MyApp.HTTP.http.download(path, ClipPathUtil.getAPPPath() + System.currentTimeMillis());
//        if (path.contains(HttpStringUtil.HeadSpeech)){
//            mPlayer = new MediaPlayer();
//            try {
//                mPlayer.setDataSource(path);
//                // setDuration();
//                mPlayer.prepareAsync();
//            } catch (IOException e) {
//                MyLog.L(MString.Params + "pathnow:IOException " + e.toString());
//                return;
//            }
//        }else{
            mPlayer=MediaPlayer.create(MyApp.activity, Uri.parse(path));

//        }

    }

    private void setDuration(){
        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()  {
            @Override
            public void onPrepared(MediaPlayer mp) {
                  duration = mPlayer.getDuration();
            }
        });
    }

    public  long getDuration(){
        if (mPlayer!=null)return mPlayer.getDuration();
       return duration;
    }
    public static void back(){
        if (intence!=null)intence.stop();
    }
    public static UPlayer getIntence(String path) {
       // path="http://218.6.173.17:8080/designpass/upload/projectRadio/20160913/1473738997243.amr";
      //  String path1=MyApp.File.getCachaPath()+ path;
    //    MyApp.HTTP.http.download(HttpStringUtil.getPath(path),path1);
//        path="http://218.6.173.17:8181/designpass/upload/projectRadio/20170704/1499162311590.mp3";
        if (intence == null) {
            intence = new UPlayer(HttpStringUtil.getPath(path));
        } else {
            if (!HttpStringUtil.getPath(path).equals(intence.path)){
                intence.stop();
                intence = null;
                intence = new UPlayer(HttpStringUtil.getPath(path));
            }

        }
        return intence;
    }


    @Override
    public boolean start() {
        try {
            //设置要播放的文件
            if (mPlayer == null) {
//                mPlayer = new MediaPlayer();
//                mPlayer.setDataSource(path);
                MyLog.L(MString.Params + "pathnow:创建"+path);
                mPlayer=MediaPlayer.create(MyApp.activity, Uri.parse(path));
            }
            if (mPlayer.isPlaying()) {
                MyLog.L(MString.Params + "pathnow:暂停"+mPlayer.isPlaying());
                mPlayer.pause();
            } else {

//                mPlayer.setataSource(path);
                //播放
                MyLog.L(MString.Params + "pathnow:播放 mPlayer="+mPlayer+" isPlaying="+mPlayer.isPlaying());
                mPlayer.start();
            }
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    MyLog.L(MString.Params + "pathnow:stop" + path);
                    mp.seekTo(0);
                    try {


                        stop();
                    } catch (Exception e) {
                    }

                }
            });
        } catch (Exception e) {
            MyLog.L(MString.Params + "prepare() failed" + e.toString());
            stop();
        }


        return false;
    }

    @Override
    public boolean stop() {
        try {

           if (mPlayer!=null)mPlayer.stop();
            if (mPlayer!=null) mPlayer.release();
            mPlayer = null;
            intence=null;
        } catch (Exception e) {
        }

        return false;
    }
}
//private final String TAG = UPlayer.class.getName();
//public String path;
//private static UPlayer intence;
//public MediaPlayer mPlayer;
//
//    public UPlayer(String path) {
//        this.path = path;
//        mPlayer = new MediaPlayer();
//        init(mPlayer, path);
//        MyLog.L(MString.UPlayer + "UPlayer:create=" + path);
//    }
//
//
//    public static UPlayer getIntence(String path) {
//        if (intence == null) {
//            intence = new UPlayer(HttpStringUtil.getPath(path));
//        } else {
//            MyLog.L(MString.UPlayer + "getIntence:twice=");
//            if (intence.mPlayer==null)intence.mPlayer=new MediaPlayer();
//            intence.path=HttpStringUtil.getPath(path);
//            init(intence.mPlayer, HttpStringUtil.getPath(path));
//        }
//        return intence;
//    }
//
//    private static void init(MediaPlayer mPlayer, String path) {
//        MyLog.L(MString.UPlayer + "init:path=" + path);
//
//        if (mPlayer == null) {
//            mPlayer = new MediaPlayer();
//        }
//        if (mPlayer.isPlaying()) {
//            mPlayer.stop();
//            mPlayer.release();
//            mPlayer = null;
//            mPlayer = new MediaPlayer();
//            try {
//                mPlayer.setDataSource(path);
//            } catch (IOException e) {
//                MyLog.L(MString.UPlayer + "init:IOException=" + e.toString());
//            }
//
//        }
////        try {
////            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
////            //mPlayer.reset();
////            MyLog.L(MString.UPlayer + "init:path=" + path);
////            mPlayer.setDataSource(path);
////            mPlayer.prepare();
////        } catch (IllegalArgumentException e) {
////            MyLog.L(MString.UPlayer + "init:IllegalArgumentException=" + e.toString());
////        } catch (IllegalStateException e) {
////            MyLog.L(MString.UPlayer + "init:IllegalStateException=" + e.toString());
////        } catch (IOException e) {
////            MyLog.L(MString.UPlayer + "init:IOException=" + e.toString());
////        }
//
//
//    }
//
//    @Override
//    public boolean start() {
////        try {
////            //设置要播放的文件
////            if (mPlayer == null) {
////                mPlayer = new MediaPlayer();
////                init(mPlayer, path);
////            }
////
////            if (mPlayer.isPlaying()) {
////                mPlayer.stop();
////            } else {
////                //播放
////                mPlayer.start();
////            }
////
////        } catch (Exception e) {
////
////            MyLog.L(MString.UPlayer + "start:" + e.toString());
////        }
//
//
//
//        try {
////            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
////                @Override
////                public void onPrepared(MediaPlayer mp) {
////                    mp.start();
////                }
////            });
//            // Prepare to async playing
//            mPlayer.prepare();
//            mPlayer.start();
//        } catch (IllegalArgumentException e) {
//            MyLog.L(MString.UPlayer + "init:IllegalArgumentException=" + e.toString());
//        } catch (SecurityException e) {
//            MyLog.L(MString.UPlayer + "init:SecurityException=" + e.toString());
//        } catch (IllegalStateException e) {
//            MyLog.L(MString.UPlayer + "init:IllegalStateException=" + e.toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//            @Override
//            public void onCompletion(MediaPlayer mp) {
//                mp.stop();
//                mp.release();
//                mp = null;
//            }
//        });
//        return false;
//    }
//
//    @Override
//    public boolean stop() {
//
//
//        return false;
//    }
//
//
////    public static void doPlayVoice(String src) {
////        MediaPlayer mp = null;
////        src = HttpStringUtil.getPath(src);
////        if (mp == null) {
////            mp = new MediaPlayer();
////        }
////        // 这里就直接用mp.isPlaying()，因为不可能再报IllegalArgumentException异常了
////        if (mp.isPlaying()) {
////            mp.stop();
////            mp.release();
////            mp = null;
////            mp = new MediaPlayer();
////
////        }
////        try {
////            mp.setDataSource(src);
////            mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
////                @Override
////                public void onPrepared(MediaPlayer mp) {
////                    mp.start();
////                }
////            });
////            // Prepare to async playing
////            mp.prepareAsync();
////        } catch (IllegalArgumentException e) {
////            e.printStackTrace();
////        } catch (SecurityException e) {
////            e.printStackTrace();
////        } catch (IllegalStateException e) {
////            e.printStackTrace();
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
////
////
////        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
////            @Override
////            public void onCompletion(MediaPlayer mp) {
////                mp.stop();
////                mp.release();
////                mp = null;
////            }
////        });
////
////    }
